/*
package com.rongke.web.api;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.rongke.commons.JsonResp;
import com.rongke.commons.PageDto;
import com.rongke.mapper.ChannelMapper;
import com.rongke.model.Admin;
import com.rongke.model.Channel;
import com.rongke.model.OptionOrder;
import com.rongke.model.User;
import com.rongke.service.AdminService;
import com.rongke.service.ChannelService;

import com.rongke.service.OptionOrderService;
import com.rongke.service.UserService;
import com.rongke.utils.Md5;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.SimpleDateFormat;
import java.util.*;

*/
/**
 * @ChannelController
 * @Controller
 * @version : Ver 1.0
 *//*

@RestController
@RequestMapping(value="/api/channel")
@Transactional
@CrossOrigin
public class ChannelController {
    private Logger log = Logger.getLogger(this.getClass());
    @Autowired
    private ChannelService channelService;
    @Autowired
    private UserService userService;
    @Autowired
    private OptionOrderService optionOrderService;
    @Autowired
    private AdminService adminService;

    */
/**
     * @添加
     * @param channel
     * @return 返回值JsonResp
     *//*

    @RequestMapping(value="/add", method = RequestMethod.POST)
    public JsonResp addChannel(@RequestBody Channel channel){
        log.debug("添加");
        channelService.insert(channel);
        return JsonResp.ok(channel);
    }

    */
/**
     * @修改
     * @param channel
     * @return 返回值JsonResp
     *//*

    @RequestMapping(value="/update", method = RequestMethod.POST)
    public JsonResp updateChannel(@RequestBody Channel channel){
        log.debug("修改");
        channelService.updateById(channel);
        return JsonResp.ok(channel);
    }
    */
/**
     * @根据id查找
     * @param id
     * @return 返回值JsonResp
     *//*

    @RequestMapping(value="/selectOne", method = RequestMethod.GET)
    public JsonResp selectChannel(Long id){
        log.debug("查找");
        Channel channel = channelService.selectById(id);
        return JsonResp.ok(channel);
    }



    @RequestMapping(value = "/addChannel2",method = RequestMethod.POST)
    public JsonResp addChannel2(@RequestBody Channel channel){
           EntityWrapper login_name=new EntityWrapper();
           login_name.eq("login_name",channel.getLoginName());
           List<Channel> channelList=channelService.selectList(login_name);
        EntityWrapper user_name=new EntityWrapper();
        user_name.eq("user_name",channel.getLoginName());
           List<Admin> adminList=adminService.selectList(user_name);
            if (channelList.size()>0){
                 return JsonResp.fa("该账户已注册");
            }
            if(adminList.size()>0){
             return JsonResp.fa("该账户已在管理员里注册");
            }
          log.debug("添加");
        Map mass=adminService.findLoginUser();
        if(mass.get("user")!=null && mass.get("user")!=""){
           if (channel.getLevel()==2){
               if (channel.getRelation()!=null){
                   Channel  channels=channelService.selectById(channel.getRelation());
                   if(channels!=null){
                       if(channels.getLevel()==1){
                           SimpleDateFormat sdf = new SimpleDateFormat("ddMMMyyyyHHmmssSSS");
                           String str=sdf.format(new Date());
                           channel.setRelation(channels.getId());
                           channel.setLinkUrl(exChange(Md5.md5Encode(str)));
                           channel.setTwolevelagent(((Admin)mass.get("user")).getId()+"admin");
                           channel.setGmtDatetime(new Date());
                           channel.setUptDatetime(new Date());
                           channel.setLevel(2);
                           channel.setPassword(Md5.md5Encode(channel.getPassword()));
                           channelService.insert(channel);
                            return JsonResp.ok();
                       }else{
                           return JsonResp.fa("未找到代理");
                       }
                   }else{
                       return JsonResp.fa("未发现该目录消息");
                   }
               }else{
                   return JsonResp.fa("未选择一级目录");
               }
            }else if (channel.getLevel()==3){
                if (channel.getRelation()!=null){
                    Channel  channels=channelService.selectById(channel.getRelation());
                    if(channels!=null){
                        if(channels.getLevel()==2){
                            SimpleDateFormat sdf = new SimpleDateFormat("ddMMMyyyyHHmmssSSS");
                            String str=sdf.format(new Date());
                            channel.setRelation(channels.getId());
                            channel.setLinkUrl(exChange(Md5.md5Encode(str)));
                            channel.setTwolevelagent(((Admin)mass.get("user")).getId()+"admin");
                            channel.setLevel(3);
                            channel.setGmtDatetime(new Date());
                            channel.setUptDatetime(new Date());
                            channel.setPassword(Md5.md5Encode(channel.getPassword()));
                            channelService.insert(channel);
                            return JsonResp.ok();
                        }else{
                            return JsonResp.fa("未找到代理");
                        }
                    }else{
                        return JsonResp.fa("未发现该目录消息");
                    }
                }else{
                    return JsonResp.fa("未选择二级代理商");
                }
            }else if (channel.getLevel()==1){
                           SimpleDateFormat sdf = new SimpleDateFormat("ddMMMyyyyHHmmssSSS");
                           String str=sdf.format(new Date());
                           channel.setRelation(null);
                           channel.setLinkUrl(exChange(Md5.md5Encode(str)));
                           channel.setTwolevelagent(((Admin)mass.get("user")).getId()+"admin");
                           channel.setLevel(1);
                           channel.setGmtDatetime(new Date());
                           channel.setUptDatetime(new Date());
                           channel.setPassword(Md5.md5Encode(channel.getPassword()));
                           channelService.insert(channel);
                           return JsonResp.ok();
           }
        }else if (mass.get("channel")!=null && mass.get("channel")!=""){
                 if (((Channel)mass.get("channel")).getLevel()==1){
                       if (channel.getLevel()==1){
                           return JsonResp.fa("不可添加同级别的代理");
                       }else if(channel.getLevel()==2){
                           SimpleDateFormat sdf = new SimpleDateFormat("ddMMMyyyyHHmmssSSS");
                           String str=sdf.format(new Date());
                           channel.setRelation(((Admin)mass.get("channel")).getId());
                           channel.setLinkUrl(exChange(Md5.md5Encode(str)));
                           channel.setTwolevelagent(((Admin)mass.get("channel")).getId()+"channel");
                           channel.setLevel(2);
                           channel.setGmtDatetime(new Date());
                           channel.setUptDatetime(new Date());
                           channel.setPassword(Md5.md5Encode(channel.getPassword()));
                           channelService.insert(channel);
                           return JsonResp.ok();
                       }else if(channel.getLevel()==3){
                           if (channel.getRelation()!=null){
                               Channel  channels=channelService.selectById(channel.getRelation());
                               if(channels!=null){
                                   if(channels.getLevel()==2){
                                       SimpleDateFormat sdf = new SimpleDateFormat("ddMMMyyyyHHmmssSSS");
                                       String str=sdf.format(new Date());
                                       channel.setRelation(channels.getId());
                                       channel.setLinkUrl(exChange(Md5.md5Encode(str)));
                                       channel.setTwolevelagent(((Admin)mass.get("channel")).getId()+"channel");
                                       channel.setLevel(3);
                                       channel.setGmtDatetime(new Date());
                                       channel.setUptDatetime(new Date());
                                       channel.setPassword(Md5.md5Encode(channel.getPassword()));
                                       channelService.insert(channel);
                                       return JsonResp.ok();
                                   }else{
                                       return JsonResp.fa("未找到代理");
                                   }
                               }else{
                                   return JsonResp.fa("未发现该目录消息");
                               }
                           }else{
                               return JsonResp.fa("未选择二级代理商");
                           }
                       }
                 }else   if (((Channel)mass.get("channel")).getLevel()==2){
                     if (channel.getLevel()==1){
                         return JsonResp.fa("不可添加上级代理");
                     }else if(channel.getLevel()==2){
                         return JsonResp.fa("不可添加同级别代理");
                     }else if(channel.getLevel()==3){
                         SimpleDateFormat sdf = new SimpleDateFormat("ddMMMyyyyHHmmssSSS");
                         String str=sdf.format(new Date());
                         channel.setRelation(((Admin)mass.get("channel")).getId());
                         channel.setLinkUrl(exChange(Md5.md5Encode(str)));
                         channel.setTwolevelagent(((Admin)mass.get("channel")).getId()+"channel");
                         channel.setLevel(3);
                         channel.setGmtDatetime(new Date());
                         channel.setUptDatetime(new Date());
                         channel.setPassword(Md5.md5Encode(channel.getPassword()));
                         channelService.insert(channel);
                         return JsonResp.ok();
                     }
                 }else   if (((Channel)mass.get("channel")).getLevel()==3){
                     return JsonResp.fa("级别过低不可添加");
                 }


        }
        return JsonResp.fa("未发现登录信息");

    }




    public static String exChange(String str){
        StringBuffer sb = new StringBuffer();
        if(str!=null){
            for(int i=0;i<str.length();i++){
                char c = str.charAt(i);
                if(Character.isUpperCase(c)){
                    sb.append(Character.toLowerCase(c));
                }else if(Character.isLowerCase(c)){
                    sb.append(Character.toUpperCase(c));
                }else{
                    sb.append(c);
                }
            }
        }

        return sb.toString();
    }


    */
/****
     * 代理商管理
     * *//*

    @RequestMapping(value = "/Agentmanagement",method = RequestMethod.GET)
    public JsonResp Agentmanagement(Page page,Channel channel){
        Map map=new HashMap();
        Map mass=adminService.findLoginUser();
          if(mass.get("user")!=null && mass.get("user")!=""){
              map.put("pageNo",(page.getCurrent()-1)*page.getSize());
              map.put("pageSize",page.getSize());
              map.put("channel",channel);
              List<Channel>  channelList=channelService.selectListAll(map);
              Integer  count=channelService.selectListAllcount(map);
              return JsonResp.ok(new PageDto(page.getCurrent(),page.getSize(),channelList,count));
           }else if (mass.get("channel")!=null && mass.get("channel")!=""){
                if (((Channel)mass.get("channel")).getLevel()==1){
                    map.put("pageNo",(page.getCurrent()-1)*page.getSize());
                    map.put("pageSize",page.getSize());
                    channel.setId(((Channel)mass.get("channel")).getId());
                    map.put("channel",channel);
                    List<Channel>  channelList=channelService.selectListAll(map);
                    Integer  count=channelService.selectListAllcount(map);
                    if (!channelList.isEmpty()){
                         for (Channel cha:channelList){
                                EntityWrapper entityWrapper=new EntityWrapper();
                                 entityWrapper.eq("relation",cha.getId());
                                 entityWrapper.eq("status",1);
                                 entityWrapper.eq("level",2);
                                List<Channel> channelList11=channelService.selectList(entityWrapper);
                                if (!channelList11.isEmpty()){
                                        cha.setTwochannelss(channelList11);
                                          for (Channel chan:channelList11){
                                              EntityWrapper entityWr=new EntityWrapper();
                                              entityWr.eq("relation",chan.getId());
                                              entityWr.eq("status",1);
                                              entityWr.eq("level",3);
                                              List<Channel> channelList1s=channelService.selectList(entityWr);
                                              if (!channelList1s.isEmpty()){
                                                  chan.setThreechannl(channelList1s);
                                              }
                                              }
                                }
                         }
                    }


                    return JsonResp.ok(new PageDto(page.getCurrent(),page.getSize(),channelList,count));
                }else if (((Channel)mass.get("channel")).getLevel()==2){
                    map.put("pageNo",(page.getCurrent()-1)*page.getSize());
                    map.put("pageSize",page.getSize());
                    channel.setId(((Channel)mass.get("channel")).getId());
                    map.put("channel",channel);
                    List<Channel>  channelList=channelService.selectListAll(map);
                    Integer  count=channelService.selectListAllcount(map);
                    if (!channelList.isEmpty()){
                        for (Channel cha:channelList){
                            EntityWrapper entityWrapper=new EntityWrapper();
                            entityWrapper.eq("relation",cha.getId());
                            entityWrapper.eq("status",1);
                            entityWrapper.eq("level",3);
                            List<Channel> channelList11=channelService.selectList(entityWrapper);
                            if (!channelList11.isEmpty()){
                                cha.setTwochannelss(channelList11);
                            }
                        }
                    }
                    return JsonResp.ok(new PageDto(page.getCurrent(),page.getSize(),channelList,count));


                }else if (((Channel)mass.get("channel")).getLevel()==3){
                    map.put("pageNo",(page.getCurrent()-1)*page.getSize());
                    map.put("pageSize",page.getSize());
                    channel.setId(((Channel)mass.get("channel")).getId());
                    map.put("channel",channel);
                    List<Channel>  channelList=channelService.selectListAll(map);
                    Integer  count=channelService.selectListAllcount(map);
                    return JsonResp.ok(new PageDto(page.getCurrent(),page.getSize(),channelList,count));
                }
          }
          return JsonResp.fa("未发现登录信息");

    }







    */
/****
     * 查询代理商推广用户的订单
     * *//*

    @RequestMapping(value = "/selectSpread",method = RequestMethod.GET)
    public JsonResp selectSpread(Page page,Long id){
        log.debug("查找用户订单");
         Map map=new HashMap();
        map.put("pageNo",(page.getCurrent()-1)*page.getSize());
        map.put("pageSize",page.getSize());
        Channel channel=channelService.selectById(id);
        List<OptionOrder> optionOrderList=null;
        Integer sun=null;
        BigDecimal MYBJ=null;
        BigDecimal QLZJ=null;
                  if (channel.getLevel()==1){
                      EntityWrapper level1=new EntityWrapper();
                      level1.eq("channel_one_id",id);
                      List<User> userList=userService.selectList(level1);
                      if (!userList.isEmpty()){
                          List<Long> list=new ArrayList<>();
                            for (User us:userList){
                                   list.add(us.getId());
                            }
                            map.put("ids",list);
                     optionOrderList=optionOrderService.selectOrderList(map);
                     sun=optionOrderService.selectOrderListcount(map);
                     MYBJ=optionOrderService.selectOrderListMYZJ(map);
                     QLZJ=optionOrderService.selectOrderListQLZJ(map);
                      }
                  }else if(channel.getLevel()==2){
                      EntityWrapper level1=new EntityWrapper();
                      level1.eq("channel_two_id",id);
                      List<User> userList=userService.selectList(level1);
                      if (!userList.isEmpty()){
                          List<Long> list=new ArrayList<>();
                          for (User us:userList){
                              list.add(us.getId());
                          }
                          map.put("ids",list);
                          optionOrderList=optionOrderService.selectOrderList(map);
                          sun=optionOrderService.selectOrderListcount(map);
                          MYBJ=optionOrderService.selectOrderListMYZJ(map);
                          QLZJ=optionOrderService.selectOrderListQLZJ(map);
                      }
                  }else  if(channel.getLevel()==3){
                      EntityWrapper level1=new EntityWrapper();
                      level1.eq("channel_three_id",id);
                      List<User> userList=userService.selectList(level1);
                      if (!userList.isEmpty()){
                          List<Long> list=new ArrayList<>();
                          for (User us:userList){
                              list.add(us.getId());
                          }
                          map.put("ids",list);
                          optionOrderList=optionOrderService.selectOrderList(map);
                          sun=optionOrderService.selectOrderListcount(map);
                          MYBJ=optionOrderService.selectOrderListMYZJ(map);
                          QLZJ=optionOrderService.selectOrderListQLZJ(map);
                      }
                  }
                  Map mapss=new HashMap();
               //   PageDto pageDto = new PageDto(page.getCurrent(),page.getSize(),optionOrderList,0);
//                  mapss.put("optionOrderList",));
                  mapss.put("optionOrderList",optionOrderList);
                  mapss.put("sun",sun);
                  mapss.put("MYBJ",MYBJ);
                  mapss.put("QLZJ",QLZJ);
                  return JsonResp.ok(mapss);
    }


    */
/***
     * 根据id查找下面的代理
     * *//*

    @RequestMapping(value = "/gotopage",method = RequestMethod.GET)
    public JsonResp gotopage(Long id){
        Channel channel=channelService.selectById(id);
        List<Channel> channelList=null;
           if(channel.getLevel()==1){
                EntityWrapper entityWrapper=new EntityWrapper();
               entityWrapper.eq("level",2);
               entityWrapper.eq("relation",id);
              channelList=channelService.selectList(entityWrapper);
           }else if (channel.getLevel()==2){
               EntityWrapper entityWrapper=new EntityWrapper();
               entityWrapper.eq("level",3);
               entityWrapper.eq("relation",id);
               channelList=channelService.selectList(entityWrapper);
           }
           return JsonResp.ok(channelList);
    }


*/
/****
 *
 * 查找代理商级别
 * *//*

          @RequestMapping(value = "/agentlevel",method = RequestMethod.GET)
          public  JsonResp  agentlevel(Integer levr){
              List<Channel> channelList=null;
                if (levr!=null) {
                    Map map = adminService.findLoginUser();
                    EntityWrapper entityWrapper = new EntityWrapper();
                    if (map.get("user") != null && map.get("user") != "") {
                        if (levr == 2) {
                            entityWrapper.eq("level", 1);
                            entityWrapper.eq("status", 1);
                         channelList = channelService.selectList(entityWrapper);
                        } else if (levr == 3) {
                            entityWrapper.in("level", "2");
                            entityWrapper.eq("status", 1);
                          channelList = channelService.selectList(entityWrapper);
                        }
                    } else if (map.get("channel") != null && map.get("channel") != "") {
                        if (levr == 3) {
                            entityWrapper.in("level", "2");
                            entityWrapper.eq("relation", ((Channel) map.get("channel")).getId());
                            entityWrapper.eq("status", 1);
                            channelList = channelService.selectList(entityWrapper);
                        }
                    }
                }else{
                    return JsonResp.fa("错误");
                }
              return JsonResp.ok(channelList);
          }



    @RequestMapping(value = "/agentlevel1",method = RequestMethod.GET)
    public  JsonResp  agentlevel1(Integer levr){
        List<Channel> channelList=null;
        if (levr!=null) {
            Map map = adminService.findLoginUser();
            EntityWrapper entityWrapper = new EntityWrapper();
            if (map.get("user") != null && map.get("user") != "") {
                if (levr == 2) {
                    return generalagentone();
                } else if (levr == 3) {
                    return generalagenton2e(2,null);
                }
            } else if (map.get("channel") != null && map.get("channel") != "") {
                 if (((Channel)map.get("channel")).getLevel()==1){
                     if (levr == 3) {
                         return generalagenton2e(2,((Channel)map.get("channel")).getId());
                     }else{
                         return JsonResp.ok();
                     }
                 }else{
                     return JsonResp.ok();
                 }
            }
        }else{
            return JsonResp.fa("错误");
        }
        return JsonResp.fa("错误");
    }



    */
/*  *
            * * * * 代理商客户管理
            * *//*

          @RequestMapping(value = "/Customer",method = RequestMethod.GET)
          public JsonResp Customer(Page page,Channel channel){
              Map masp=new HashMap();
              masp.put("pageNo",(page.getCurrent()-1)*page.getSize());
              masp.put("pageSize",page.getSize());
                 if (channel.getId()!=null){
                       Channel channel1=channelService.selectById(channel.getId());
                     if(channel1.getLevel()==1){
                         masp.put("type",1);
                         masp.put("chaid",channel1.getId());
                     }else if(channel1.getLevel()==2){
                         masp.put("chaid",channel1.getId());
                     }else if(channel1.getLevel()==3){
                         masp.put("chaid",channel1.getId());
                     }
                 }else{
                     Map map = adminService.findLoginUser();
                     if (map.get("channel")!=null && map.get("channel")!=""){
                         if(((Channel)map.get("channel")).getLevel()==1){
                             masp.put("type",1);
                             masp.put("chaid",((Channel)map.get("channel")).getId());
                         }else if(((Channel)map.get("channel")).getLevel()==2){
                             masp.put("type",2);
                             masp.put("chaid",((Channel)map.get("channel")).getId());
                         }else if(((Channel)map.get("channel")).getLevel()==3){
                             masp.put("type",3);
                             masp.put("chaid",((Channel)map.get("channel")).getId());
                         }
                     }
                 }
              List<Channel> channelList=channelService.selectCustomer(masp);
               Integer sun=channelService.selectCustomercount(masp);
              return JsonResp.ok(new PageDto(page.getCurrent(),page.getSize(),channelList,sun));
          }



          */
/***
           * 代理商客户管理
           * *//*


          @RequestMapping(value = "/Customer2",method = RequestMethod.GET)
          public JsonResp Customer2(Page page,Channel channel){
               Map maps=adminService.findLoginUser();
              Map masp=new HashMap();
              masp.put("pageNo",(page.getCurrent()-1)*page.getSize());
              masp.put("pageSize",page.getSize());

              if (maps.get("user")!=null && maps.get("user")!=""){


              }else if (maps.get("channel")!=null && maps.get("channel")!=""){


              }










              if (channel.getId()!=null){
                  Channel channel1=channelService.selectById(channel.getId());
                  if(channel1.getLevel()==1){
                      masp.put("type",1);
                      masp.put("chaid",channel1.getId());
                  }else if(channel1.getLevel()==2){
                      masp.put("chaid",channel1.getId());
                  }else if(channel1.getLevel()==3){
                      masp.put("chaid",channel1.getId());
                  }
              }else{
                  Map map = adminService.findLoginUser();
                  if (map.get("channel")!=null && map.get("channel")!=""){
                      if(((Channel)map.get("channel")).getLevel()==1){
                          masp.put("type",1);
                          masp.put("chaid",((Channel)map.get("channel")).getId());
                      }else if(((Channel)map.get("channel")).getLevel()==2){
                          masp.put("type",2);
                          masp.put("chaid",((Channel)map.get("channel")).getId());
                      }else if(((Channel)map.get("channel")).getLevel()==3){
                          masp.put("type",3);
                          masp.put("chaid",((Channel)map.get("channel")).getId());
                      }
                  }
              }
              List<Channel> channelList=channelService.selectCustomer(masp);
              Integer sun=channelService.selectCustomercount(masp);
              return JsonResp.ok(new PageDto(page.getCurrent(),page.getSize(),channelList,sun));
          }




    */
/***
           *查一级代理
           *  *//*

           @RequestMapping(value="generalagentone",method = RequestMethod.GET)
            public JsonResp generalagentone(){
               EntityWrapper entityWrapper=new EntityWrapper();
                entityWrapper.eq("level",1);
                entityWrapper.eq("status","1");
                List<Channel> channelList=channelService.selectList(entityWrapper);
               return JsonResp.ok(channelList);
           }
    */
/***
     *查代理
     *  *//*

    @RequestMapping(value="generalagenton2e",method = RequestMethod.GET)
    public JsonResp generalagenton2e(Integer lev,Long id){
        EntityWrapper entityWrapper=new EntityWrapper();
        entityWrapper.eq("level",lev);
        if (id!=null){
            entityWrapper.eq("relation",id);
        }
        entityWrapper.eq("status","1");
        List<Channel> channelList=channelService.selectList(entityWrapper);
        return JsonResp.ok(channelList);
    }

        */
/***
        *根据一级id查二级代理
        *  *//*

        @RequestMapping(value="generalagentowe",method = RequestMethod.GET)
        public JsonResp generalagentowe(Long id){
        EntityWrapper entityWrapper=new EntityWrapper();
        entityWrapper.eq("relation",id);
        entityWrapper.eq("level",2);
        entityWrapper.eq("status","1");
        List<Channel> channelList=channelService.selectList(entityWrapper);
        return JsonResp.ok(channelList);
       }


    */
/***
     *根据二级id查三级代理
     *  *//*

    @RequestMapping(value="generalagenThree",method = RequestMethod.GET)
    public JsonResp generalagenThree(Long id){
        EntityWrapper entityWrapper=new EntityWrapper();
        entityWrapper.eq("relation",id);
        entityWrapper.eq("level",3);
        entityWrapper.eq("status","1");
        List<Channel> channelList=channelService.selectList(entityWrapper);
        return JsonResp.ok(channelList);
    }


}
*/
